home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / AIAT / Headers / Common / IACommon.h next >
Encoding:
Text File  |  1998-04-16  |  8.3 KB  |  261 lines  |  [TEXT/CWIE]

  1. // IACommon.h
  2. // Common types, constants, classes and macros used by IA code.
  3. //    Copyright:    © 1994 - 1998 by Apple Computer, Inc., all rights reserved.
  4.  
  5. #pragma once
  6. #ifndef IACommon_h
  7. #define IACommon_h
  8.  
  9. #pragma import on
  10. #if PRAGMA_STRUCT_ALIGN
  11.     #pragma options align=power
  12. #endif
  13.  
  14.  
  15. #ifndef IA_BEGIN_IMPORTS
  16. // the following are used when building shared libraries to define imports & exports
  17. // by default, they're defined with a no-op pragma
  18.  
  19. //#define IA_BEGIN_IMPORTS    mark includes
  20. //#define IA_END_IMPORTS        mark includes
  21. //#define IA_BEGIN_EXPORTS    mark includes
  22. //#define IA_END_EXPORTS        mark includes
  23.  
  24. #define IA_BEGIN_IMPORTS    import on 
  25. #define IA_END_IMPORTS        import reset
  26. #define IA_BEGIN_EXPORTS    export on
  27. #define IA_END_EXPORTS        export off
  28.  
  29. #endif
  30.  
  31. // some inline members generate out-of-line definitions which cause
  32. // conflicts when building a shared library.  the following macros
  33. // allow inlines to be turned off or on, so that library clients see
  34. // only the out-of-line definitions, while the library may use the inline.
  35. #ifdef IA_NO_INLINES
  36. // use the out-of-line definitions from the library
  37. #define IA_INLINE
  38. #define IA_INLINE_DEF();        
  39. #define IA_INLINE_DEF_BODY(BODY);        
  40. #else
  41. // let definitions inline
  42. #define IA_INLINE            inline
  43. #define IA_INLINE_DEF()    {}
  44. #define IA_INLINE_DEF_BODY(BODY)    { BODY; }
  45. #endif
  46.  
  47. // define IA_NO_EXCEPTIONS if your compiler does not yet support exceptions
  48.  
  49.  
  50. //#pragma IA_BEGIN_IMPORTS
  51. #include <stdlib.h>
  52. #include <string.h>
  53. //#pragma IA_END_IMPORTS
  54.  
  55. #pragma IA_BEGIN_EXPORTS
  56.  
  57. //// ** Primitive types **
  58. /// These names are chosen to be short and to not conflict with others used on the Mac.
  59. typedef unsigned char    byte;
  60. typedef byte            Byte;
  61. typedef long            int32;
  62. typedef unsigned long    uint32;
  63. typedef unsigned short    uint16;
  64. typedef uint16             UInt16;
  65.  
  66. typedef void*    (*IAAllocFptr)(size_t size);  // ptr is a user defined struct
  67. typedef void    (*IADeallocFptr)(void* object); // ptr is a user defined struct
  68.  
  69. //// ** IA globals **
  70. extern uint32            IADiskBlockSize;        // determines default size for most i/o
  71. extern IAAllocFptr        IAAllocationFunc;        // allocation function call back
  72. extern IADeallocFptr    IADeAllocationFunc;        // deallocation function call back
  73.  
  74. //// ** IAMalloc -- all IA memory allocation is done through this **
  75.  
  76. // IAMalloc & IAFree may be used in place of malloc() and free()
  77. void*                    IAMalloc(size_t size);
  78. void                    IAFree(void* object);
  79. // IAMallocSied & IAFreeSized may be used when the size is known at free time.
  80. // They may be faster and/or use less memory.
  81. void*                    IAMallocSized(size_t size);
  82. void                    IAFreeSized(void* object, size_t size);
  83.  
  84. /// handy macros for memory allocation
  85.  
  86. // IAMallocStruct & IAFreeStruct
  87. #define                    IAMallocStruct(TYPE)            (TYPE*)IAMallocSized(sizeof(TYPE))
  88. #define                    IAFreeStruct(MEM,TYPE)            IAFreeSized(MEM, sizeof(TYPE))
  89.  
  90. // IAMallocArray & IAFreeArray
  91. // Caution: this should not be used for classes with virtual member functions (e.g. IAObjects)
  92. // Caution: nor should it be used for classes whose default ctor or dtor do anything
  93. #define                    IAMallocArray(TYPE,SIZE)        (TYPE*)IAMalloc(sizeof(TYPE) * (SIZE))
  94. // we distinguish array deletion to enable substitution of 'delete[]' rather than 'delete'
  95. #define                    IAFreeArray                        IAFree
  96. // variants for sized allocation
  97. #define                    IAMallocArraySized(TYPE,SIZE)    (TYPE*)IAMallocSized(sizeof(TYPE) * (SIZE))
  98. #define                    IAFreeArraySized(MEM,TYPE,SIZE)    IAFreeSized(MEM, sizeof(TYPE) * (SIZE))
  99.  
  100. // in the debug libraries, prints some info about memory usage to the standard output
  101. void                    IAReportMemoryUsage();
  102.  
  103. //// ** IAStruct: a base for IA structs -- allocated by IAMalloc **
  104.  
  105. struct                    IAStruct {
  106. public:
  107.     virtual             ~IAStruct();
  108.     void*                operator new(size_t size);//        { return IAMalloc(size); }
  109.     void                operator delete(void* object);//    { IAFree(object); }
  110. };
  111.  
  112.  
  113. //// ** IAObject: a base class for Information Access objects **
  114.  
  115. class                    IAObject : public IAStruct {
  116. public:
  117.                         IAObject() {}
  118.     // Ensure objects have a virtual destructor.  This enables IADeleteOnUnwind et. al.
  119.     IA_INLINE virtual    ~IAObject()                                IA_INLINE_DEF()
  120.     void*                operator new(size_t size)                { return IAMalloc(size); } // { return IAMallocSized(size); }
  121.     IA_INLINE void        operator delete(void* obj, size_t /*size*/) IA_INLINE_DEF_BODY(IAFree(obj)) //IA_INLINE_DEF_BODY(IAFreeSized(obj, size))
  122. private:
  123.     // Copy constructors cause no end of confusion in C++, so we don't use them.
  124.     // Instead, in any class with a destructor, we:
  125.     // . specify but don't define one (to generate link errors if it is ever called)
  126.     // . make it private so that it can't be specialized.
  127.                         IAObject(IAObject&);
  128. };
  129.  
  130. // enable stack-based deletion of pointers to IAObjects
  131. // this ensures that the destructor is called even when the stack is unwound on exception
  132. // the typical usage is something like:
  133. //   { Foo* foo = FunctionReturningFooStar();
  134. //     IADeleteOnUnwind delFoo(foo);
  135. //     < code using foo> }
  136. class                    IADeleteOnUnwind {
  137. public:
  138.                         IADeleteOnUnwind(IAObject* o) : object(o) {}
  139.                         ~IADeleteOnUnwind();            // delete object;
  140.     IAObject*            object;
  141. private:
  142.     void*                operator new(size_t size);        // stack allocate only
  143. };
  144.  
  145. // permit stack-allocated variable-sized arrays
  146. // the typical usage is something like:
  147. //   { Foo* foos = IAMallocArray(<variable>, <size>);
  148. //     IADeleteArrayOnUnwind delFoos(foos);
  149. //     < code using foos> }
  150. class                    IADeleteArrayOnUnwind {
  151. public:
  152.                         IADeleteArrayOnUnwind(void* a);    // : array(a) {}
  153.                         ~IADeleteArrayOnUnwind();        // { IAFreeArray(array); }
  154.     void*                array;
  155. private:
  156.     void*                operator new(size_t size);        // stack allocate only
  157. };
  158.  
  159. // permit stack-allocated variable-sized arrays of pointers to IAObjects
  160. // the typical usage is something like:
  161. //   { Foo** foos = IAMallocArraySized(Foo*, <size>);
  162. //     < initialize foos >
  163. //     IADeletePointerArrayOnUnwind delFoos(foos, <size>);
  164. //     < code using foos> }
  165. class            IADeletePointerArrayOnUnwind {
  166. public:
  167.                 IADeletePointerArrayOnUnwind(IAObject* a[], uint32 l);
  168.                 ~IADeletePointerArrayOnUnwind();
  169.     IAObject**            array;
  170.     uint32                length;
  171. private:
  172.     void*                operator new(size_t size);        // stack allocate only
  173. };
  174.  
  175.  
  176. //// ** IA Exceptions **
  177.  
  178. // define IA_NO_EXCEPTIONS if your compiler does not yet support exceptions
  179. #ifndef IA_NO_EXCEPTIONS
  180.  
  181. class IAException
  182. {
  183.   public:
  184.                             IAException( const char* message);
  185.     virtual                     ~IAException();
  186.     
  187. //    virtual void            Throw() const;
  188.     virtual const char*     What() const;
  189.     virtual const char*        GetLocation() const;
  190.     virtual int32             GetCode() const;
  191.     
  192.             void             SetLocation (const char* location);
  193.             void             SetCode (int32 code);
  194.     
  195.   private:
  196.   
  197.       virtual const char*     what() const;
  198.       
  199.             char            fMessage[250];
  200.             char            fLocation[50];
  201.             int32            fCode;
  202. };
  203.  
  204.  
  205. #else
  206. // until compilers have better exception support, just throw integers
  207. typedef const int32        IAException;
  208. #endif
  209.  
  210. typedef const int32     IAExceptionCode;
  211.  
  212. // don't inline calls to throw() -- makes code smaller & faster!
  213. void IAThrowException(IAException exception);    // { throw(exception); }
  214.  
  215. //
  216.  
  217. void _IAAssertionFunc(const char*, int, const char*, const char*, IAExceptionCode code);
  218. #define IAAssertion(expression, message, code) \
  219.     ( (expression) ? (void) 0 : (_IAAssertionFunc(__FILE__, __LINE__, #expression, message, code)) )
  220. //
  221.  
  222. // define IAThrow to shadow definitions in this file
  223. #ifndef IAThrow
  224.  
  225.  
  226. // define IA_NO_EXCEPTIONS if your compiler does not yet support exceptions
  227. #ifndef IA_NO_EXCEPTIONS
  228. // exceptions are ANSI by default
  229. #define IAThrow(EXCEPTION)    IAThrowException(EXCEPTION) // throw(exception)
  230. #define IATry                try
  231. #define IACatch(BINDING)    catch (BINDING)
  232.  
  233. #else
  234.  
  235. // definitions for compilers that don't support exceptions
  236. #define IAThrow(EXCEPTION)    IAThrowException(EXCEPTION) // exit(EXCEPTION)
  237. #define IATry                if (true)
  238. #define IACatch(BINDING)    for (BINDING = 0; false; )
  239.  
  240. #endif
  241. #endif
  242.  
  243. IAExceptionCode                IAAssertionFailure = 'VTWN';
  244.  
  245. // handy macros which conditionally throw exceptions
  246. #define IAAssert(VALUE)                    if (!(VALUE))    (_IAAssertionFunc(__FILE__, __LINE__, #VALUE, "IAAssert", IAAssertionFailure))
  247. #define IAThrowIf(VALUE, EXCEPTION)        if (  VALUE )    (_IAAssertionFunc(__FILE__, __LINE__, #VALUE, "IAThrowIf", EXCEPTION))
  248. #define IAThrowIfNot(VALUE, EXCEPTION)    if (!(VALUE))    (_IAAssertionFunc(__FILE__, __LINE__, #VALUE, "IAThrowIfNot", EXCEPTION))
  249.  
  250. typedef bool TProgressFn(float percent, void* data);
  251.  
  252. #pragma IA_END_EXPORTS
  253.  
  254. #if PRAGMA_STRUCT_ALIGN
  255.     #pragma options align=reset
  256. #endif
  257.  
  258. #pragma import reset
  259.  
  260. #endif
  261.